package com.ruoyi.flink.chapten5;

import org.apache.flink.api.java.functions.KeySelector;

/**
 * Copyright (c) 2020-2030 尚硅谷 All Rights Reserved
 * <p>
 * Project:  FlinkTutorial
 * <p>
 * Created by  wushengran
 */

import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class Trans4TSimpleAggreationTest {
	public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        DataStreamSource<Event> stream = env.fromElements(
               new Event("Mary", "./home", 1000L),
               new Event("Bob", "./cart", 2000L),
               new Event("Alice", "./prod?id=10", 3000L),
               new Event("Bob", "./prod?id=1", 3300L),
               new Event("Bob", "./home", 3500L),
               new Event("Alice", "./prod?id=200", 3200L),
               new Event("Bob", "./prod?id=2", 3800L),
               new Event("Bob", "./prod?id=3", 4200L)
        );

       // 按键分组之后进行聚合,提取当前用户最近一次访问数据
       stream.keyBy(new KeySelector<Event, String>(){
		@Override
		public String getKey(Event value) throws Exception {
			return value.user;
		}  
       }).max("timestamp").print("max:");
       
       stream.keyBy(data -> data.user).maxBy("timestamp").print("maxBy:");
       
        env.execute();
    }
}
